<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * This license is also available via the world-wide-web at
 * http://itdapps.ncsu.edu/bsd.txt
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to itappdev@ncsu.edu so we can send you a copy immediately.
 *
 * @package    Index_Controller
 * @category   Controller
 * @copyright  Copyright (c) 2007 NC State University Office of      
 *             Information Technology
 * @license    http://itdapps.ncsu.edu/bsd.txt  BSD License
 * @version    SVN: $Id: $
 */

/**
 * Main controller for the application
 *
 * @package    Index_Controller
 * @category   Controller
 * @copyright  Copyright (c) 2007 NC State University Office of      
 *             Information Technology
 */
class TagsController extends Zend_Controller_Action
{
          
    /**
     * shows the homepage
     *
     */
    
	public function editTagAction()
    {       
        $get = Zend_Registry::get('getFilter');

        $tags = new Tags();
        
        $thisTag = $tags->find($get->tagId);
        $form = $tags->form($thisTag->toArray());
        
        $messages = array();
        
        if ($this->_request->isPost()) {
            if ($form->isValid($_POST)) {
                
                $data = array(
                    'tagId'      	=> $thisTag->tagId,
                    'name'			=> $form->getValue('name'),
                    'description'   => $form->getValue('description')
                );
                
                $dba = $tags->getAdapter();
                
                $dba->beginTransaction();
                
                try {
                    $tags->update($data, null);
                } catch (Exception $e) {
                    $dba->rollback();
                    throw $e;
                }
		                        
		        $logOptions = array(
		         	'attributeName' => 'taskId',
		          	'attributeId'   => $thisTag->tagId,
		        );
                
                $this->_helper->log(Zend_Log::INFO, 'Tag ' . $thisTag->tagId . ' was modified', $logOptions);     
                $this->_helper->flashMessenger->addMessage('The tag was successfully modified.');
                
                $this->_helper->redirector->gotoRoute(array('module' => 'default', 'controller' => 'tasks', 'action' => 'index'), 'default', true);
            } else {
                $messages[] = 'There was an error modifying this tag.';
            }
        }
        
        $this->view->form = $form;
        $this->view->messages = $messages;
        $this->_helper->pageTitle('Edit Tag : ' . $thisTag->name);
    }
    
    public function deleteTagAction()
    {
    	$get = Zend_Registry::get('getFilter');
    	$tags = new Tags();
    	
    	if(!$tags->isTagOwner(Zend_Auth::getInstance()->getIdentity()->accountId, $get->tagId)) {
    		throw new Ot_Exception_Data('You are not the owner of this tag.');
    	}
    	
    	$thisTag = $tags->find($get->tagId);
    	$tagArray = $thisTag->toArray();
    	    	
    	$form = $tags->deleteTagForm($tagArray);
    	
    	foreach($tagArray as $t) {
    		$name = $t['name'];
    	}
    		    	    	
	    if ($this->_request->isPost()) {
	            if ($form->isValid($_POST)) {

	            	$tags->delete($tags->getAdapter()->quoteInto('tagId = ?', $form->getValue('tagId')));
			                        
			        $logOptions = array(
			         	'attributeName' => 'tagId',
			          	'attributeId'   => $form->getValue('tagId'),
			        );
	                
	                $this->_helper->log(Zend_Log::INFO, 'Tag ' . $form->getValue('tagId') . ' was deleted', $logOptions);     
	                $this->_helper->flashMessenger->addMessage('The tag was successfully deleted.');
	                
	                $this->_helper->redirector->gotoRoute(array('module' => 'default', 'controller' => 'tasks', 'action' => 'index'), 'default', true);
	            } else {
	                $messages[] = 'There was an error deleting this tag.';
	            }
		}
    	
    	
    	$this->_helper->pageTitle('Delete Tag');
    	$this->view->form = $form;
    	
    }
}